package com.example.sxy.serialportdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;
import java.security.InvalidParameterException;

import android_serialport_api.ComBean;
import android_serialport_api.SerialHelper;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void BtClick(View v){
        SerialHelper serialHelper = new SerialHelper("/dev/ttyS2", 9600) {
            @Override
            protected void onDataReceived(ComBean ComRecData) {
                Toast.makeText(MainActivity.this, ComRecData.sRecTime+"success", Toast.LENGTH_SHORT);
            }
        };
        openComPort(serialHelper);
    }
    private void openComPort(SerialHelper serialHelper){
        try
        {
            serialHelper.open();
            showMessage("success");
        } catch (SecurityException e) {
            showMessage("没有权限!");
        } catch (IOException e) {
            showMessage("未知错误!");
        } catch (InvalidParameterException e) {
            showMessage("参数错误!");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    private void showMessage(String sMsg)
    {
        Toast.makeText(this.getApplicationContext(),
                sMsg, Toast.LENGTH_LONG).show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
